package com.example.chinesechess;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class BoardView extends View {
    private String redText;  
    private int value;  
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;

  
    //a paint object to render the redText  
    private Paint paint;  
    private float x;
    private float y;
  
    public BoardView(Context context, AttributeSet attrs)  
    {  
        super(context, attrs);  
        mScaleDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
        	public boolean onScale(ScaleGestureDetector detector) {
                mScaleFactor *= detector.getScaleFactor();

                // Don't let the object get too small or too large.
                mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

                invalidate();
                return true;
            }     	
        });

        //initialize the paint object  
        paint = new Paint();  
        //set the color to red  
        paint.setColor(Color.RED);  
  
        //initialize the redText String with the attribute with the same name at the XML file  
        //redText = attrs.getAttributeValue(null, "redText");  
        //initialize the value integer with the attribute with the same name at the XML file  
        //value = attrs.getAttributeIntValue(null, "value", 0);  
    }  

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);
        x = ev.getX();
        y = ev.getY();
        this.invalidate();
        return true;
    }

    
    @Override  
    protected void onDraw(Canvas canvas)  
    {  
        super.onDraw(canvas);  
        
        canvas.drawText(""+x+","+y, 10, 100, paint);  
        
        double width = this.getWidth();
        double height = this.getHeight();
        
        double boxSide = width/9;
        double horiMargin = 0.5*boxSide;
        double vertiMargin = (height-9*boxSide)/2;
        //new AlertDialog.Builder(this.getContext()).setMessage(""+)

        canvas.save();
        //canvas.scale(mScaleFactor, mScaleFactor);

        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        for(int i = 0;i<8;i++) {
        	for(int j=0;j<8;j++) {
        		float left = (float)(horiMargin+i*boxSide);
        		float top = (float)(vertiMargin+(j>=4? j+1:j)*boxSide);
        		float right = (float)(horiMargin+(i+1)*boxSide);
        		float bottom = (float)(vertiMargin+(j>=4?j+2:j+1)*boxSide);
        		canvas.drawRect(left,top,right,bottom, paint);
        	}
        }
        canvas.drawLine((float)(horiMargin+3*boxSide), (float)(vertiMargin),(float)( horiMargin+5*boxSide), (float)(vertiMargin+2*boxSide), paint);
        canvas.drawLine((float)(horiMargin+3*boxSide), (float)(vertiMargin+2*boxSide),(float)( horiMargin+5*boxSide), (float)(vertiMargin), paint);
        canvas.drawLine((float)(horiMargin+3*boxSide), (float)(vertiMargin+7*boxSide),(float)( horiMargin+5*boxSide), (float)(vertiMargin+9*boxSide), paint);
        canvas.drawLine((float)(horiMargin+3*boxSide), (float)(vertiMargin+9*boxSide),(float)( horiMargin+5*boxSide), (float)(vertiMargin+7*boxSide), paint);
        
        canvas.save();
        canvas.translate((float)(horiMargin+boxSide), (float)(vertiMargin+2*boxSide));
        drawMark(false,false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+7*boxSide), (float)(vertiMargin+2*boxSide));
        drawMark(false,false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)horiMargin,(float)(vertiMargin+3*boxSide));
        drawMark(true, false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+2*boxSide), (float)(vertiMargin+3*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+4*boxSide), (float)(vertiMargin+3*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+6*boxSide), (float)(vertiMargin+3*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+8*boxSide), (float)(vertiMargin+3*boxSide));
        drawMark(false, true, boxSide,canvas);
        canvas.restore();
        
        
        canvas.save();
        canvas.translate((float)(horiMargin+boxSide), (float)(vertiMargin+7*boxSide));
        drawMark(false,false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+7*boxSide), (float)(vertiMargin+7*boxSide));
        drawMark(false,false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)horiMargin,(float)(vertiMargin+6*boxSide));
        drawMark(true, false, boxSide, canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+2*boxSide), (float)(vertiMargin+6*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+4*boxSide), (float)(vertiMargin+6*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+6*boxSide), (float)(vertiMargin+6*boxSide));
        drawMark(false, false, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+8*boxSide), (float)(vertiMargin+6*boxSide));
        drawMark(false, true, boxSide,canvas);
        canvas.restore();
        canvas.save();
        canvas.translate((float)(horiMargin+4*boxSide), (float)(vertiMargin));
        drawPiece(0.3*boxSide, canvas);
        canvas.restore();
        //Bitmap bm;
        //bm.
        canvas.restore();
        
    }  
    
    private void drawPiece(double radius, Canvas canvas) {
    	
    }
    
    private void drawMark(boolean leftCorner, boolean rightCorner,double boxSide, Canvas canvas) {
    	float gapLength = (float)(0.05*boxSide);
    	float lineLength = (float)(0.2*boxSide);
    	if(leftCorner) {
    		canvas.drawLine(gapLength, gapLength+lineLength, gapLength, gapLength, paint);
    		canvas.drawLine(gapLength, gapLength, gapLength+lineLength, gapLength, paint);
    		canvas.drawLine(gapLength, -1*gapLength, gapLength, -1*(gapLength+lineLength), paint);
    		canvas.drawLine(gapLength, -1*gapLength, gapLength+lineLength, -1*gapLength, paint);
    	}
    	else if(rightCorner) {
    		canvas.drawLine(-1*gapLength, gapLength+lineLength, -1*gapLength, gapLength, paint);
    		canvas.drawLine(-1*gapLength, gapLength, -1*(gapLength+lineLength), gapLength, paint);
    		canvas.drawLine(-1*gapLength, -1*gapLength, -1*gapLength, -1*(gapLength+lineLength), paint);
    		canvas.drawLine(-1*gapLength, -1*gapLength, -1*(gapLength+lineLength), -1*gapLength, paint);   		
    	}
    	else {
    		canvas.drawLine(gapLength, gapLength+lineLength, gapLength, gapLength, paint);
    		canvas.drawLine(gapLength, gapLength, gapLength+lineLength, gapLength, paint);
    		canvas.drawLine(gapLength, -1*gapLength, gapLength, -1*(gapLength+lineLength), paint);
    		canvas.drawLine(gapLength, -1*gapLength, gapLength+lineLength, -1*gapLength, paint);    
    		canvas.drawLine(-1*gapLength, gapLength+lineLength, -1*gapLength, gapLength, paint);
    		canvas.drawLine(-1*gapLength, gapLength, -1*(gapLength+lineLength), gapLength, paint);
    		canvas.drawLine(-1*gapLength, -1*gapLength, -1*gapLength, -1*(gapLength+lineLength), paint);
    		canvas.drawLine(-1*gapLength, -1*gapLength, -1*(gapLength+lineLength), -1*gapLength, paint);  
    	}
    }


}
